import java.util.*;

public class Problem {
    public static void main1(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int t=scanner.nextInt();
        while (t-->0){
            int n=0,s=0;
            int num=scanner.nextInt();
            while (num>0&&num!=10&&num!=100&&num!=1000){
                n+=num%10*Math.pow(10,s);
                s++;
                num/=10;
            }
            if(num!=10||n<2){
                System.out.println("NO");
            }
            else if(num==10&&n>=2){
                System.out.println("YES");
            }
        }
    }

    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            int[] a = new int[n];
            HashSet<Integer> occupied = new HashSet<>();
            boolean allFollowed = true;

            for (int j = 0; j < n; j++) {
                a[j] = scanner.nextInt();
            }

            for (int j = 0; j < n; j++) {
                if (occupied.isEmpty()) {
                    occupied.add(a[j]);
                } else {
                    boolean hasNeighbor = false;
                    if (a[j] > 1 && occupied.contains(a[j] - 1)) {
                        hasNeighbor = true;
                    }
                    if (a[j] < n && occupied.contains(a[j] + 1)) {
                        hasNeighbor = true;
                    }
                    if (!hasNeighbor) {
                        allFollowed = false;
                        break;
                    }
                    occupied.add(a[j]);
                }
            }

            if (allFollowed) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
        scanner.close();
    }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int t=scanner.nextInt();
        while (t-->0){
            int n=scanner.nextInt();
            long[] nums=new long[n];
            for (int i = 0; i < n; i++) {
                nums[i]=scanner.nextLong();
            }
            int m=scanner.nextInt();
            while (m-->0){
                Map<Long,Character> map1=new HashMap<>();
                Map<Character,Long> map2=new HashMap<>();
                String s= scanner.next();
                boolean flag=true;
                if(s.length()!=n){
                    System.out.println("NO");
                    continue;
                }
                else {
                    for (int i = 0; i < n; i++) {
                        if(!map1.containsKey(nums[i])&&!map2.containsKey(s.charAt(i))){
                            map1.put(nums[i],s.charAt(i));
                            map2.put(s.charAt(i),nums[i]);
                        }
                        else if(map1.containsKey(nums[i])&&!map2.containsKey(s.charAt(i))&&flag){
                            System.out.println("NO");
                            flag=false;
                        } else if (!map1.containsKey(nums[i])&&map2.containsKey(s.charAt(i))&&flag) {
                            System.out.println("NO");
                            flag=false;
                        }
                    }
                    if(flag){
                        System.out.println("YES");
                    }
                }
            }
        }
    }

}